import axios from 'common/js/token/request'

export function loginByUsername(username, password) {
  const data = {
    username,
    password
  }
  return axios.post('/api/OAuth/login', data)
    .then((res) => {
      return Promise.resolve(res.data)
    })
    .catch((err) => {
      console.log(err)
      return Promise.reject(err)
    })
}

export function logout() {
  return axios.post('/api/Accounts/logout')
    .then(res => {
      return Promise.resolve(res.data)
    })
    .catch(err => {
      console.log(err)
    })
}

export function getUserInfo() {
  return axios.get('/api/Accounts/MyProfile')
    .then((res) => {
      return Promise.resolve(res.data)
    })
    .catch(err => {
      console.log(err)
    })
}

export function postAccessToken(network, token, email) {
  const url = `/api/ExternalAuth/${network}-login`
  const data = {
    accessToken: token,
    email: email
  }
  return axios.post(url, data)
    .then(res => {
      return Promise.resolve(res.data)
    })
    .catch(err => {
      console.log(err)
    })
}
